//
//  ViewController.m
//  TableViewDemo
//
//  Created by 张伊辉 on 13-10-11.
//  Copyright (c) 2013年 张伊辉. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
       
    
    self.navigationItem.title = @"学习Tableview";
    
    
    
    muArrData = [[NSMutableArray alloc] init];
    result = [[NSMutableArray alloc] init];

    for (int i = 0; i<26; i++) {
        
        
        for (int j = 0; j<5; j++) {
            
            
            [muArrData addObject:[NSString stringWithFormat:@"%c%d",'A'+i,j]];

        }
       
    }
    indexArr = [[NSMutableArray alloc] init];
    [indexArr addObject:UITableViewIndexSearch];
    for (int i = 0; i<26; i++) {
        [indexArr addObject:[NSString stringWithFormat:@"%c",'A'+i]];
    }
    [indexArr addObject:@"end"];
    
    

    myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-44)];
    myTable.delegate = self;
    myTable.dataSource = self;
    [self.view addSubview:myTable];
    [myTable release];
    
    endLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    endLabel.backgroundColor = [UIColor clearColor];
    endLabel.text = @"end";
    endLabel.textAlignment = NSTextAlignmentCenter;
    [myTable setTableFooterView:endLabel];
    [endLabel release];
   
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"暂时没用" style:UIBarButtonItemStyleBordered target:self action:@selector(leftAction)] autorelease];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(rightAciton)]autorelease];
    
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;
    [myTable setTableHeaderView:searchBar];
    [searchBar release];
    
    UISearchDisplayController *searchdisplay = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
    searchdisplay.delegate = self;
    searchdisplay.searchResultsDelegate = self;
    searchdisplay.searchResultsDataSource = self;
    
    
	// Do any additional setup after loading the view, typically from a nib.
}
-(void)leftAction{
    
    
    
}
-(void)rightAciton{
    
    static int click = 0;
    click ++;
    if (click%2==0) {
        [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
        
        [myTable setEditing:NO animated:YES];


    }else{
        
        [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
        
        [myTable setEditing:YES animated:YES];

    }
    
}



#pragma mark tabledelegate


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    if (myTable == tableView) {
        return indexArr;

    }
    return nil;
}// return list of section titles to display in section index view (e.g. "ABCD...Z#")
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    
    if (title == UITableViewIndexSearch) {
        [myTable scrollRectToVisible:searchBar.frame animated:YES];
        /* 滚动到 UITable的self.searchBar.frame区域 */
        return -1;
        /* return -1表示不要让系统滚动任何段 */
    }
    if ([title isEqualToString:@"end"]) {
        
       [myTable scrollRectToVisible:endLabel.frame animated:YES];
        return -1;
    }
    /* title目前就是 A-Z */
    
    NSInteger section;
    for (int i= 0; i<[indexArr count]; i++) {
        if ([[indexArr objectAtIndex:i] isEqualToString:title]) {
            section = i;
        }
    }
    
    
    
    /* 上ALPHA字符串中去找title的 子串
     然会一个NSRange对象 NSRange.location是在子串中的位置 */
    return section-1;
    
   
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (tableView == myTable) {
        return [NSString stringWithFormat:@"%c", 'A'+section];

    }
    return @"搜索结果如下:";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (tableView == myTable) {
        return 26;

    }
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (tableView == myTable) {
        
        return 5;
        
    }else{
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@",searchBar.text];
        /* self.searchBar.text 取得搜索框的文本 */
        /* "SELF contains [cd] A"
         返回当前包含了 A字符串  [cd] c表示不区分大小写 d不区分
         */
        [result removeAllObjects];
        NSArray *array = [muArrData filteredArrayUsingPredicate:predicate];
        [result addObjectsFromArray:array];
                
        return  [result count];
    }
   
    

    
    
    
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *strIndefier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIndefier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIndefier];
    }
    if (tableView == myTable) {
        cell.textLabel.text = [NSString stringWithFormat:@"%c%d",'A'+indexPath.section,indexPath.row];

    }else{
        cell.textLabel.text = [result objectAtIndex:indexPath.row];

    }
    cell.accessoryType = UITableViewCellStyleValue1;
    return cell;
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
      
        [muArrData removeObjectAtIndex:indexPath.row];
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
               
       
    }
    
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
    [muArrData exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}


#pragma mark searchDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    
    NSLog(@"search");
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    NSLog(@"searchText is %@",searchText);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
